<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const posts=[
            {title:'I Love JavaScript',author:'菠萝头',id:1},
            {title:'CSS!',author:'吸氧羊',id:2},
            {title:'Dev tools webpack',author:'CEO'}
        ];
        // posts.push()
        // localStorage.setItem('items',JSON.stringify(posts));
        const authors=[
            {name:'菠萝头',hometown:'赣州',bio:'面向字节跳动编程'}
        ]
        function getPostById(id){
            // id 找到文章
            // const post=posts.find(post=>post.id===id);
            // const {author}=post;
            // console.log(authors.find(item=>item.name==author));
            // return post
            return new Promise((resolve,reject)=>{
                // resolve()
                console.log('------')
                setTimeout(()=>{
                    const post=posts.find(post=>post.id==id);
                    // console.log(post);
                    if(post){
                        resolve(post);// 成功执行resolve方法
                    }else{
                        let err=new Error('没有数据')
                        reject(err);// 失败执行reject方法
                    }
                    
                },200)
            })
        }
        getPostById(10)
            .then((data)=>{//状态为fulfilled时执行
                console.log(data,'/////');
                console.log('这是成功操作');
            }
            // ,
            //     function(reason){ //状态为rejected时执行
            //     console.log(reason);
            //     console.log('这是失败的操作');
            // }
            // catch和第二个参数类似，都是在错误的时候执行
            )
            .catch((err)=>{//状态为rejected时执行
                console.log(err.message);
                console.log('这是失败的操作');
            })
    </script>
</body>
</html>